package com.itheima.app.job;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.AnnouncementVo;
import com.itheima.service.db.AnnouncementService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.List;

@Component
public class MyBrithday {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Reference
    AnnouncementService announcementService;


    //TODO 演示
    @Scheduled(cron = "0/5 * * * * ?")
    public void findCommentsByBirthday() {

        List<UserInfo> userInfoList= announcementService.announcementBirthday();

        if (CollUtil.isNotEmpty(userInfoList)) {
            for (UserInfo userInfo : userInfoList) {
                //3.4 封装UserCommentVo,放入新集合中
                AnnouncementVo announcementVo = new AnnouncementVo();
                announcementVo.setId(userInfo.getId().toString());
                announcementVo.setTitle("HappyBirthday");
                announcementVo.setDescription("生日快乐!!!");
                announcementVo.setCreateDate(String.valueOf(System.currentTimeMillis()));

                stringRedisTemplate.opsForValue().set(ConstantUtil.ANNCOUNCEMENT_BRITHDAY+userInfo.getId(), JSON.toJSONString(announcementVo), Duration.ofDays(1));

            }
        }
        System.out.println("定时器已执行");
    }

}
